module.exports = function (app, mongoose) {
  app.get("/search", function (req, res) {
    var Patient = mongoose.model("Patient");
    var searchtext = JSON.parse(req.query.searchData)
    var phoneObj = searchtext
    // 获取传过来数组
    if (phoneObj.phone) {
      phoneObj.phone = phoneObj.phone.split(/,|\s|，/)
    }
    var obj = {}
    for (i in searchtext) {
      if (i == 'name' || i == 'phone' || i == 'weixin' || i == 'qq' || i == 'region' || i == 'zhenduan' || i == 'doctor')
        obj[i] = new RegExp(searchtext[i])
    }
    Patient.find(Array.isArray(phoneObj.phone) ? phoneObj : obj, function (err, docs) {
      if (err) {
        console.log("err:", err);
        res.json({
          state: 1,
          msg: "数据库错误"
        });
        return
      } else {
        if (docs == []) {
          res.json({
            status: 1,
            msg: '没有此患者信息'
          })
          return
        } else {
          res.json({
            status: 0,
            data: docs,
            msg: '查询成功'
          })
          return
        }
      }
    })
  })

}
